from aiohttp import web
from user import users

# 添加好友接口
async def add_friend(request):
    data = await request.json()
    username = data.get('username')
    friend = data.get('friend')
    if not username or not friend:
        return web.json_response({'success': False, 'msg': '参数不完整'}, status=400)
    if username == friend:
        return web.json_response({'success': False, 'msg': '不能添加自己为好友'}, status=400)
    if username not in users or friend not in users:
        return web.json_response({'success': False, 'msg': '用户不存在'}, status=400)
    if friend in users[username]['friends']:
        return web.json_response({'success': False, 'msg': '已是好友'}, status=400)
    users[username]['friends'].add(friend)
    users[friend]['friends'].add(username)
    return web.json_response({'success': True, 'msg': '添加好友成功'})

# 删除好友接口
async def remove_friend(request):
    data = await request.json()
    username = data.get('username')
    friend = data.get('friend')
    if not username or not friend:
        return web.json_response({'success': False, 'msg': '参数不完整'}, status=400)
    if username not in users or friend not in users:
        return web.json_response({'success': False, 'msg': '用户不存在'}, status=400)
    if friend not in users[username]['friends']:
        return web.json_response({'success': False, 'msg': '不是好友'}, status=400)
    users[username]['friends'].remove(friend)
    users[friend]['friends'].remove(username)
    return web.json_response({'success': True, 'msg': '删除好友成功'})
